package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.AddressBook;
import com.itheima.service.AddressBookService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增用户对应的地址簿
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId()); //获取当前登录用户id 并设置给地址簿user_id字段
        log.info("地址簿添加信息：{}",addressBook.toString());
        //
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }


    /**
     * 设置默认地址簿
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public  R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        //构造条件构造器
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        // id相等  地址默认改为 0
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        queryWrapper.set(AddressBook::getIsDefault,0);

        // SQL：update address_book set is_default = 0 where user_id = ?
        // 用户id 对应地址簿 多个 user_id   全部地址修改成 0 非默认
        addressBookService.update(queryWrapper);

        //设置 默认地址
        addressBook.setIsDefault(1);
        // 修改
        // SQL：update address_book set is_default = 1 where id = ?   通过Address_book表 主键id修该状态
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }


    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址，id为：{}",id);

        AddressBook addressBook = addressBookService.getById(id);
        //判断
        if(addressBook != null){
            return R.success(addressBook);
        }else {
            return R.error("该对象不存在！");
        }

    }


    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getdefauleAddress(){
        log.info("查询默认地址......");
        //构造条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());  // user_id  = id
        queryWrapper.eq(AddressBook::getIsDefault,1);                // is_default = 1

        // SQL: select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper); //查询一个用户的默认地址
        //判断
        if(addressBook != null){
            return R.success(addressBook);
        }else{
            return R.error("没有该对象");
        }

    }

    /**
     *  查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> getListAddressBook(AddressBook addressBook){ //空对象
        log.info("当前地址簿对象携带的id：{}",addressBook.getId());

        addressBook.setUserId(BaseContext.getCurrentId());  //addressBook空对象设置 user_id值

        //构造条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(),AddressBook::getUserId,addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL : select * from address_book where user_id = ? order by update_time desc
        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

}
